<template>
  <div class="home-page">
    <section class="search-section">
      <div class="container">
        <div class="search-container">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索洗护服务..."
            class="search-input"
            @keyup.enter="handleSearch"
          >
            <template #append>
              <el-button @click="handleSearch">
                <el-icon><Search /></el-icon>
              </el-button>
            </template>
          </el-input>
          <div class="hot-keywords">
            <span>热门搜索：</span>
            <el-button
              v-for="keyword in hotKeywords"
              :key="keyword"
              link
              @click="searchKeyword = keyword; handleSearch()"
            >
              {{ keyword }}
            </el-button>
          </div>
        </div>
      </div>
    </section>

    <div class="main-layout container">
      <!-- 左侧服务分类菜单 -->
      <aside class="service-categories">
        <h3 class="category-title">服务分类</h3>
        <el-menu
          :default-active="activeCategory"
          class="category-menu"
          @select="handleCategorySelect"
        >
          <el-menu-item index="all">
            <el-icon><Menu /></el-icon>
            <span>全部服务</span>
          </el-menu-item>
          
          <!-- 洗衣服务 -->
          <el-menu-item index="LAUNDRY">
            <el-icon><Suitcase /></el-icon>
            <span>洗衣服务</span>
          </el-menu-item>
          
          <!-- 洗鞋服务 -->
          <el-menu-item index="SHOE_CLEANING">
            <el-icon><Goods /></el-icon>
            <span>洗鞋服务</span>
          </el-menu-item>
          
          <!-- 萌宠洗护 -->
          <el-menu-item index="PET_CLEANING">
            <el-icon><Star /></el-icon>
            <span>萌宠洗护</span>
          </el-menu-item>
          
          <!-- 包包清洗 -->
          <el-menu-item index="BAG_CLEANING">
            <el-icon><Goods /></el-icon>
            <span>包包清洗</span>
          </el-menu-item>
          
          <!-- 床品清洗 -->
          <el-menu-item index="BEDDING_CLEANING">
            <el-icon><House /></el-icon>
            <span>床品清洗</span>
          </el-menu-item>
          
          <!-- 洗车服务 -->
          <el-menu-item index="CAR_CLEANING">
            <el-icon><Van /></el-icon>
            <span>洗车服务</span>
          </el-menu-item>
          
          <!-- 上门清洗 -->
          <el-menu-item index="HOME_CLEANING">
            <el-icon><Service /></el-icon>
            <span>上门清洗</span>
          </el-menu-item>
          
          <!-- 管道疏通 -->
          <el-menu-item index="PIPE_DREDGING">
            <el-icon><Watermelon /></el-icon>
            <span>管道疏通</span>
          </el-menu-item>
        </el-menu>
        
        <!-- 服务推荐 -->
        <div class="service-recommendation">
          <h4>推荐服务</h4>
          <div class="recommendation-list">
            <div 
              v-for="service in recommendedSidebarServices" 
              :key="service.id"
              class="recommendation-item"
              @click="goToServiceDetail(service.id)"
            >
              <div class="recommendation-image">
                <img :src="getServiceImage(service)" :alt="service.title" />
              </div>
              <div class="recommendation-info">
                <div class="recommendation-title">{{ service.title }}</div>
                <div class="recommendation-price">¥{{ service.price }}</div>
              </div>
            </div>
          </div>
        </div>
      </aside>
      
      <!-- 中间内容区域 -->
      <div class="content-area">
        <h2>热门服务</h2>
        <div class="services-grid">
          <div
            v-for="service in displayedServices"
            :key="service.id"
            class="service-card"
            @click="goToServiceDetail(service.id)"
          >
            <div class="service-image">
              <img :src="getServiceImage(service)" :alt="service.title" />
              <div v-if="service.hasDiscount" class="discount-badge">
                {{ calculateDiscount(service) }}折
              </div>
            </div>
            <div class="service-content">
              <h4 class="service-title">{{ service.title }}</h4>
              <p class="service-description">{{ service.description }}</p>
              <div class="service-meta">
                <span class="merchant-name">{{ service.merchantName }}</span>
                <span class="service-category">{{ getCategoryLabel(service.category) }}</span>
              </div>
              <div class="service-footer">
                <div class="price-info">
                  <span class="current-price">¥{{ service.price }}</span>
                  <span v-if="service.originalPrice && service.originalPrice > service.price"
                        class="original-price">¥{{ service.originalPrice }}</span>
                </div>
                <div class="rating-info">
                  <el-rate
                    v-model="service.rating"
                    disabled
                    size="small"
                    show-score
                    text-color="#ff9900"
                  />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Search, Menu, Suitcase, Goods, House, Van, Service, Star, Watermelon } from '@element-plus/icons-vue'

const router = useRouter()

// 搜索相关
const searchKeyword = ref('')
const hotKeywords = ref(['洗衣', '洗鞋', '窗帘清洗', '上门取送', '干洗'])

// 分类相关
const activeCategory = ref('all')

// 模拟数据
const allServices = ref([
  {
    id: 1,
    title: '高级干洗服务',
    description: '专业干洗各类衣物，去除顽固污渍',
    price: 99,
    originalPrice: 129,
    category: 'LAUNDRY',
    merchantName: '洁衣坊',
    rating: 4.5,
    hasDiscount: true
  },
  {
    id: 2,
    title: '专业洗鞋服务',
    description: '各类鞋子的专业清洗和保养',
    price: 79,
    category: 'SHOE_CLEANING',
    merchantName: '鞋博士',
    rating: 4.2
  },
  {
    id: 3,
    title: '宠物美容洗护',
    description: '专业宠物洗澡、美容和护理',
    price: 129,
    category: 'PET_CLEANING',
    merchantName: '萌宠之家',
    rating: 4.8
  },
  {
    id: 4,
    title: '奢侈品包包清洗',
    description: '各类奢侈品包包的精细清洗',
    price: 199,
    originalPrice: 299,
    category: 'BAG_CLEANING',
    merchantName: '奢品护理',
    rating: 4.7,
    hasDiscount: true
  },
  {
    id: 5,
    title: '床品四件套清洗',
    description: '床单、被套等床品的深度清洁',
    price: 89,
    category: 'BEDDING_CLEANING',
    merchantName: '居家清洁',
    rating: 4.3
  },
  {
    id: 6,
    title: '汽车精洗服务',
    description: '汽车内外深度清洁和护理',
    price: 159,
    category: 'CAR_CLEANING',
    merchantName: '车之洁',
    rating: 4.6
  },
  {
    id: 7,
    title: '上门家居清洁',
    description: '专业团队上门提供家居清洁服务',
    price: 199,
    category: 'HOME_CLEANING',
    merchantName: '到家服务',
    rating: 4.4
  },
  {
    id: 8,
    title: '管道疏通服务',
    description: '专业疏通各类下水管道',
    price: 99,
    category: 'PIPE_DREDGING',
    merchantName: '管道通',
    rating: 4.1
  }
])

const recommendedSidebarServices = ref([
  {
    id: 1,
    title: '高级干洗服务',
    price: 99,
    imageUrl: '/images/services/laundry.jpg'
  },
  {
    id: 2,
    title: '专业洗鞋服务',
    price: 79,
    imageUrl: '/images/services/shoe.jpg'
  },
  {
    id: 3,
    title: '宠物美容洗护',
    price: 129,
    imageUrl: '/images/services/pet.jpg'
  }
])

// 计算属性
const displayedServices = computed(() => {
  let filtered = [...allServices.value]
  
  // 应用分类过滤
  if (activeCategory.value !== 'all') {
    filtered = filtered.filter(service => service.category === activeCategory.value)
  }
  
  // 应用搜索关键词
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase()
    filtered = filtered.filter(service => 
      service.title.toLowerCase().includes(keyword) ||
      service.description.toLowerCase().includes(keyword) ||
      service.merchantName.toLowerCase().includes(keyword)
    )
  }
  
  return filtered
})

// 方法
const handleSearch = () => {
  console.log('搜索关键词:', searchKeyword.value)
}

const handleCategorySelect = (category) => {
  activeCategory.value = category
  console.log('选择分类:', category)
}

const getServiceImage = (service) => {
  return service.imageUrl || '/images/default-service.jpg'
}

const calculateDiscount = (service) => {
  if (!service.originalPrice || service.originalPrice <= service.price) return 100
  return Math.round((service.price / service.originalPrice) * 100)
}

const getCategoryLabel = (category) => {
  const categoryMap = {
    LAUNDRY: '洗衣服务',
    SHOE_CLEANING: '洗鞋服务',
    PET_CLEANING: '萌宠洗护',
    BAG_CLEANING: '包包清洗',
    BEDDING_CLEANING: '床品清洗',
    CAR_CLEANING: '洗车服务',
    HOME_CLEANING: '上门清洗',
    PIPE_DREDGING: '管道疏通'
  }
  return categoryMap[category] || '未知服务'
}

const goToServiceDetail = (serviceId) => {
  router.push(`/service/${serviceId}`)
}

// 生命周期钩子
onMounted(() => {
  console.log('HomePage组件已挂载')
})
</script>

<style scoped>
.home-page {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.search-section {
  background-color: #fff;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.search-container {
  max-width: 800px;
  margin: 0 auto;
}

.search-input {
  width: 100%;
}

.hot-keywords {
  margin-top: 10px;
  color: #666;
}

.main-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 20px;
  margin-top: 20px;
}

.service-categories {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.category-title {
  padding: 15px;
  margin: 0;
  border-bottom: 1px solid #eee;
}

.category-menu {
  border-right: none;
}

.service-recommendation {
  padding: 15px;
  border-top: 1px solid #eee;
}

.recommendation-list {
  margin-top: 10px;
}

.recommendation-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  cursor: pointer;
  transition: all 0.3s;
}

.recommendation-item:hover {
  background-color: #f5f7fa;
}

.recommendation-image {
  width: 60px;
  height: 60px;
  margin-right: 10px;
}

.recommendation-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.recommendation-info {
  flex: 1;
}

.recommendation-title {
  font-size: 14px;
  margin-bottom: 5px;
}

.recommendation-price {
  color: #ff6b6b;
  font-weight: bold;
}

.content-area {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.services-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.service-card {
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s;
  cursor: pointer;
}

.service-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.service-image {
  position: relative;
  height: 200px;
}

.service-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.discount-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 4px 8px;
  border-radius: 4px;
  background-color: #ff6b6b;
  color: #fff;
  font-size: 12px;
  font-weight: bold;
}

.service-content {
  padding: 15px;
}

.service-title {
  margin: 0 0 10px;
  font-size: 16px;
  font-weight: bold;
}

.service-description {
  margin: 0 0 10px;
  font-size: 14px;
  color: #666;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.service-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 12px;
  color: #999;
}

.service-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.price-info {
  display: flex;
  align-items: baseline;
  gap: 5px;
}

.current-price {
  color: #ff6b6b;
  font-size: 18px;
  font-weight: bold;
}

.original-price {
  color: #999;
  text-decoration: line-through;
  font-size: 14px;
}

.rating-info {
  display: flex;
  align-items: center;
  gap: 5px;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .main-layout {
    grid-template-columns: 1fr;
  }
  
  .service-categories {
    display: none;
  }
}
</style>